package cn.piggy.controller;

import cn.piggy.model.answer.AnswerRequestBody;
import cn.piggy.model.translation.Query;
import cn.piggy.model.translation.TranRequestBody;
import cn.piggy.service.AnswerExecutorService;
import cn.piggy.service.TranslationExecutorService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

@Controller
public class AiController {

    private final Logger logger = LoggerFactory.getLogger(AiController.class);
    @Resource
    private AnswerExecutorService answerExecutorService;
    @Resource
    private TranslationExecutorService translationExecutorService;

    @RequestMapping("/chat01")
    public void chat01(@RequestBody(required = false) AnswerRequestBody requestBody, HttpServletResponse response){
        try {
            String json01 = "{\n" +
                    "  \"messages\": [\n" +
                    "    {\"role\":\"user\", \"content\": \"写首诗\"}\n" +
                    "  ]\n" +
                    "}";
            ObjectMapper objectMapper = new ObjectMapper();
            AnswerRequestBody body = objectMapper.readValue(json01, AnswerRequestBody.class);
            answerExecutorService.asyncExecute(body, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("/chat02")
    public String chat02(@RequestBody(required = false) AnswerRequestBody requestBody) throws IOException {
        String json02 = "{\n" +
                "   \"messages\": [\n" +
                "    {\"role\":\"user\",\"content\":\"介绍一下你自己\"}\n" +
                "   ]\n" +
                "}";
        ObjectMapper objectMapper = new ObjectMapper();
        AnswerRequestBody body = objectMapper.readValue(json02, AnswerRequestBody.class);
        return answerExecutorService.syncExecute(body);
    }

    @ResponseBody
    @RequestMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
//        byte[] bytes = file.getBytes();

        InputStream fis = file.getInputStream();
        byte[] dataBytes = new byte[fis.available()];
        int read = fis.read(dataBytes);
        fis.close();
        logger.info("read --> " + read);

        String base64Str = Base64.getEncoder().encodeToString(dataBytes);
        String param = "{\"from\":\"zh\",\"to\":\"en\",\"input\":{\"content\":\""+base64Str+"\",\"format\":\"pdf\",\"filename\":\"简历.pdf\"},\"output\":{\"filename_prefix\":\"mttest_after\",\"formats\":[\"pdf\"]}}";
        ObjectMapper objectMapper = new ObjectMapper();
        TranRequestBody body = objectMapper.readValue(param, TranRequestBody.class);
        return translationExecutorService.syncExecuteTranCreate(body);
    }

    @ResponseBody
    @RequestMapping("/query")
    public String queryFile(@RequestBody Query query) {
        try {
            return translationExecutorService.syncExecuteTranQuery(query);
        }catch (Exception e){
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/downloadFile")
    public String downloadFile(@RequestBody Query query) {
        try {
            return translationExecutorService.syncExecuteTranQuery(query);
        }catch (Exception e){
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/test1/a/b/c")
    public String test1() {
        return "/test1/a/b/c";
    }

    @ResponseBody
    @RequestMapping("/a/b/c")
    public String test2() {
        return "/a/b/c";
    }

    @ResponseBody
    @RequestMapping("/test1/a/b/c")
    public String test3() {
        return "/a/b/c";
    }
}
